import { dictionaries } from "@/api/config"
let dicMap = window.sessionStorage

// let date = new Date()
// let year = date.getFullYear()
// let ndArr = []
// for (let i = 0; i < 5; i++) {
//   ndArr.push({
//     label: year - i,
//     value: String(year - i)
//   })
// }
// let runStateArr = [
//   {
//     label: '等待中',
//     value: '0'
//   },
//   {
//     label: '运行中',
//     value: '1'
//   },
//   {
//     label: '运行完成',
//     value: '2'
//   },
//   {
//     label: '运行失败',
//     value: '3'
//   }
// ]
// let shzt = [
//   {
//     label: '审核中',
//     value: '0'
//   },
//   {
//     label: '审核通过',
//     value: '1'
//   },
//   {
//     label: '审核未通过',
//     value: '-1'
//   }
// ]
// dicMap.setItem('nd', JSON.stringify(ndArr))
// dicMap.setItem('runState', JSON.stringify(runStateArr))
// // 0：等待中 1：运行中 2：运行完成 3：运行失败
// dicMap.setItem('shzt', JSON.stringify(shzt))

function diction(resolve, reject, key, val) {
  return new Promise((resolve, reject) => {
    dictionaries(key)
      .then(res => {
        let data = res.data
        if (data.success) {
          data.data.map(i => {
            if (i.codeValue !== "") {
              i.label = i.codeValue
              i.value = i.codeValue
              return i
            }
          })
          dicMap.setItem(key, JSON.stringify(data.data))
          if (val) {
            let res
            res = data.data.filter(i => {
              return i.value === val
            })
            if (res.length) {
              res = res[0].label
            } else {
              res = val
            }
            resolve(res)
          } else {
            resolve(data.data)
          }
        } else {
          // eslint-disable-next-line prefer-promise-reject-errors
          reject()
        }
      })
      .catch(res => {
        console.log(res)
      })
  })
}

export function doCreate(key) {
  return new Promise((resolve, reject) => {
    if (dicMap.getItem(key)) {
      resolve(JSON.parse(dicMap.getItem(key)))
    } else {
      diction(resolve, reject, key).then(res => {
        resolve(res)
      })
    }
  })
}

export function getDicTab(key, val) {
  let res
  if (val === null || val === undefined) return ""
  if (dicMap.getItem(key)) {
    res = JSON.parse(dicMap.getItem(key)).filter(i => {
      return i.value === val
    })
    if (res.length) {
      res = res[0].label
    } else {
      res = val
    }
    return res
  }
  // eslint-disable-next-line no-undef
  diction(resolve, reject, key, val)
}
export function getDicTabProms(key, val) {
  return new Promise((resolve, reject) => {
    let res
    if (val === null || val === undefined) resolve("")
    if (dicMap.getItem(key)) {
      res = JSON.parse(dicMap.getItem(key)).filter(i => {
        // eslint-disable-next-line eqeqeq
        return i.value == val
      })
      if (res.length) {
        res = res[0].label
      } else {
        res = val
      }
      resolve(res)
    } else {
      diction(resolve, reject, key, val)
    }
  })
}

// 数字金额转化为大写
export function moneyText(money) {
  let list = money.split(".")
  if (list.length > 2) {
    return ""
  }
  let digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]
  let unit = ["亿", "万", "仟", "佰", "拾", ""]
  let text = getText(String(Number(list[0]))) + "元"
  if (list.length === 2 && list[1].substring(0, 2) !== "00") {
    // eslint-disable-next-line eqeqeq
    if (list[1].charAt(0) != "0") {
      text += digit[list[1].charAt(0)] + "角"
    }
    // eslint-disable-next-line eqeqeq
    if (list[1].charAt(1) != "0") {
      text += digit[list[1].charAt(1)] + "分"
    }
  } else {
    text += "整"
  }
  return text

  function getText(m) {
    let str = ""
    let start = 0
    let num = [9, 5, 4, 3, 2, 1]
    if (m.length === 1) {
      return digit[m]
    }
    num.map((n, i) => {
      if (m.length >= n) {
        let dig = m.substring(start, m.length - n + 1)
        // 为0时，不加单位，避免出现类似壹佰零拾贰元的情况
        str += getText(dig) + unit[Number(dig) ? i : 5]
        start += m.length - n + 1 - start
      }
    })
    return str
  }
}
export function handleEmpty(data) {
  data.map(i => {
    if (i.children) {
      if (i.children.length) {
        handleEmpty(i.children)
      } else {
        delete i.children
      }
    }
  })
  return data
}

export function handleDel(data, row) {
  let rows = row
  data.map(i => {
    if (i.id === rows.id) {
      data.splice(data.indexOf(rows), 1)
      // return data
    } else {
      // eslint-disable-next-line no-lonely-if
      if (i.children && i.children.length > 0) {
        handleDel(i.children, rows)
      }
      // return data
    }
  })
  return data
}

export function getDiction(options, value) {
  let arry = options.filter((item) => {
    return item.value === value
  })
  if (arry && arry.length > 0) {
    return arry[0].label
  }
  return ""
}

export function getDictionSelf(options, value, key, label) {
  let arry = options.filter((item) => {
    return item[key] === value
  })
  if (arry && arry.length > 0) {
    return arry[0][label]
  }
  return ""
}

export function formatTime(t, _date) {
  var date = new Date(_date)
  var o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(t)) {
    t = t.replace(
      RegExp.$1,
      (String(date.getFullYear())).substr(4 - RegExp.$1.length)
    )
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(t)) {
      t = t.replace(
        RegExp.$1,
        RegExp.$1.length === 1
          ? o[k]
          : ("00" + o[k]).substr((String(o[k])).length)
      )
    }
  }
  return t
}


// 写成标准的方法(数组是object的一种)：
export function getObjectKeys(object) {
  var keys = [];
  for (var property in object) { keys.push(property); }
  return keys;
}
